//
//  WXPScoreView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/6.
//

#import "WXPScoreView.h"

@implementation WXPScoreView

#pragma mark - lazy
- (UIView *)viewBack
{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
        _viewBack.userInteractionEnabled = true;
    }
    return  _viewBack;
}
- (UILabel *)labelTitle
{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel  new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.numberOfLines = 0;
        _labelTitle.lineSpace = 3;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.font = [UIFont boldSystemFontOfSize:18];
    }
    return  _labelTitle;
}
- (UIView *)viewBtnBack
{
    if (_viewBtnBack == nil) {
        _viewBtnBack = [UIView  new];
        _viewBtnBack.backgroundColor = [UIColor clearColor];
        CGFloat btnWidth = 40+20;
        CGFloat btnHeight = 41;
        _viewBtnBack.widthHeight = XY(btnWidth*5, btnHeight);
        for (int i = 0; i < 5; i ++) {
            UIButton * starBtn = [UIButton new];
            starBtn.widthHeight = XY(btnWidth, btnHeight);
            starBtn.leftTop = XY(btnWidth*i, 0);
            starBtn.selected = false;
            [starBtn setImage:[UIImage imageNamed:@"icon-score-start-normal"] forState:UIControlStateNormal];
            [starBtn setImage:[UIImage imageNamed:@"icon-score-start-select"] forState:UIControlStateSelected];
            starBtn.tag = i;
            [starBtn addTarget:self action:@selector(starBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [_viewBtnBack addSubview:starBtn];
        }
    }
    return  _viewBtnBack;
}
- (UIButton *)btnEntregar
{
    if (_btnEntregar == nil) {
        _btnEntregar = [UIButton  new];
        _btnEntregar.backgroundColor = COLOR_PRIMARY;
        _btnEntregar.userInteractionEnabled = false;
        _btnEntregar.alpha = 0.5;
        [_btnEntregar setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnEntregar setTitle:@"Entregar" forState:UIControlStateNormal];
        _btnEntregar.titleLabel.fontNum = 14;
        _btnEntregar.tag = 1;
        [_btnEntregar addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnEntregar;
}
- (UIView *)viewTextViewBack
{
    if (_viewTextViewBack == nil) {
        _viewTextViewBack = [UIView  new];
        _viewTextViewBack.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
        _viewTextViewBack.userInteractionEnabled = true;
    }
    return  _viewTextViewBack;
}
- (UITextView *)contentTextView{
    if (!_contentTextView) {
        _contentTextView = [UITextView new];
        _contentTextView.backgroundColor = [UIColor clearColor];
        _contentTextView.delegate = self;
        _contentTextView.font = [UIFont systemFontOfSize:12];
        _contentTextView.textColor = COLOR_BLACK;
    }
    return _contentTextView;
}
- (UILabel *)labelPlaceholder{
    if (_labelPlaceholder == nil) {
        _labelPlaceholder = [UILabel new];
        _labelPlaceholder.numberOfLines = 0;
        _labelPlaceholder.lineSpace = 2;
        _labelPlaceholder.textColor = COLOR_CONTENT;
        _labelPlaceholder.fontNum = 12;
    }
    return _labelPlaceholder;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.score = 0;
        [self addSubView];
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [self addGestureRecognizer:tapGestureRecognizer];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.viewBtnBack];
    [self.viewBack addSubview:self.btnEntregar];
    [self.viewBack addSubview:self.viewTextViewBack];
    [self.viewTextViewBack addSubview:self.contentTextView];
    [self.viewTextViewBack addSubview:self.labelPlaceholder];
    
    //初始化页面
    [self resetViewWithModel:nil];
}

#pragma mark - textView delegate
- (void)textViewDidChange:(UITextView *)textView{
    self.labelPlaceholder.hidden = isStr(textView.text);
}

#pragma mark - 点击事件
- (void)starBtnClick:(UIButton *)sender{
    for (UIButton * btn in self.viewBtnBack.subviews) {
        btn.selected = false;
        if(btn.tag <= sender.tag){
            btn.selected = true;
        }
    }
    self.score = sender.tag;
    self.btnEntregar.userInteractionEnabled = true;
    self.btnEntregar.alpha = 1;
}

#pragma mark - 手势点击
- (void)tapClick:(UITapGestureRecognizer * )tap{
    [self endEditing:true];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
        {
            if(self.blockSubmitScore){
                self.blockSubmitScore();
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新view
- (void)resetViewWithModel:(id)model{
    self.viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    
    [self.labelTitle fitTitle:@"Le agradeceríamos si califica\nesta aplicación" variable:0];
    self.labelTitle.centerXTop = XY(self.viewBack.width/2, 50);
    
    self.viewBtnBack.centerXTop = XY(self.viewBack.width/2, self.labelTitle.bottom+20);
    
    // 内容
    self.viewTextViewBack.width = self.viewBack.width-30*2;
    self.viewTextViewBack.height = (128);
    [GlobalMethod setRoundView:self.viewTextViewBack color:[UIColor clearColor] numRound:16 width:0];
    self.viewTextViewBack.centerXTop = XY(self.viewBack.width/2, self.viewBtnBack.bottom+12);
    
    // textView
    self.contentTextView.widthHeight = XY(self.viewTextViewBack.width-2*Ratio(9), self.viewTextViewBack.height-Ratio(6)*2);
    self.contentTextView.leftTop = XY(Ratio(9), Ratio(6));
    
    self.btnEntregar.widthHeight = XY(self.viewBack.width-30*2, BUTTON_HEIGHT);
    [GlobalMethod setRoundView:self.btnEntregar color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
    self.btnEntregar.centerXTop = XY(self.viewBack.width/2, self.viewTextViewBack.bottom+30);
    
    [self.labelPlaceholder fitTitle:@"Dar retroalimentación positiva mejorará su tasa de aprobación de pedidos（opcional）" variable:(self.viewTextViewBack.width-2*Ratio(15))];
    self.labelPlaceholder.leftTop = XY(Ratio(15), Ratio(13));
    
    self.viewBack.height = self.btnEntregar.bottom+40;
    self.viewBack.centerXCenterY = XY(SCREEN_WIDTH/2, self.height/2-Ratio(15));
}





@end
